#coding=utf-8
from method.driver import Proxy
import random  # 取随机数
from config.conf import proxiesDEF
import time
from config.conf import proxiesDEF, Referer_list
from method.readCheckAsin import readCheckAsinTxt

from time import sleep
get_goods_num = 1

checkAsinArr = readCheckAsinTxt()

def getAsin():
    proxies_list = ['221.122.91.60:80', '125.123.127.251:9000', '125.73.220.18:49128', '106.12.201.174:8080', '125.123.124.192:9000', '125.123.126.137:9000', '125.123.65.5:9000', '27.195.216.21:8118', '222.128.9.235:33428', '125.123.121.214:9000', '114.249.113.151:9000', '123.56.74.221:80', '223.156.112.100:9000', '47.104.172.108:8118', '125.123.124.145:9000', '221.122.91.65:80', '122.136.212.132:53281', '125.123.44.147:9000', '218.108.175.15:80', '52.83.202.3:8118', '47.107.158.219:8000', '124.205.155.157:9090', '182.35.81.10:9999', '45.125.32.181:3128', '182.35.86.175:9999', '1.198.72.162:9999', '163.204.242.30:9999', '125.123.44.81:9000', '61.160.210.223:808', '119.178.101.18:8888', '221.122.91.61:80', '123.132.232.254:61017', '114.225.29.112:8118', '43.254.168.56:53281', '69.160.173.3:8000', '123.163.97.154:9999', '163.204.244.166:9999', '163.204.243.219:9999', '163.204.245.109:9999', '125.123.66.161:9000', '134.175.55.15:8888', '182.35.80.251:9999', '125.123.141.95:9000', '163.125.70.239:8888', '218.60.8.83:3129', '125.65.79.60:3311', '116.52.100.233:8888', '182.122.205.72:8118', '106.9.169.41:808', '175.43.34.17:9999']

    ip = proxies_list[random.randint(0, len(proxies_list)-1)]
    driver = Proxy(ip).getdriver()
    driver.get('https://sellercentral.amazon.com/ap/signin?openid.return_to=https%3A%2F%2Fsellercentral.amazon.com%2Fmessaging%2Fcontact%3ForderID%3D112-7937445-3778642%26marketplaceID%3DATVPDKIKX0DER&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.assoc_handle=sc_na_amazon_v2&openid.mode=checkid_setup&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&ssoResponse=eyJ6aXAiOiJERUYiLCJlbmMiOiJBMjU2R0NNIiwiYWxnIjoiQTI1NktXIn0.sZb2x2yBzMwy-2CLb5Eht1aHNTmbvAuZbDZt9q2gLST9pLjk1aXtpQ.h_Jln9Kb3kh3ChLb.r-5SgWtnwePl53-J7vBbkxzFQGilgAvpzugDIDqBGKaH949pS41xe2iZ-7aBespauXwQR4IchCCGkSHxYai3WBRU9oLfRNpt6y5DxqDgjNG95OXUyzqGO5ecKf63VJQ4DdN425ykC4aEjWgwa_qEOsTV1wbx83-TXNn8E2LuzRq3-06hJsdGFeCpQCe8JklRi6_LwERRDEFTd6_VHnDY_vijGP0yeE0nUXY3_IMIpbv6eq1KDVdAnCct4nVv0ep8khdpfw.V_NK6QVgCpZMS8-uPERKFg')
    print('111')
    sleep(95)
    # driver.implicitly_wait(random.uniform(122.5, 139.3))
    print('222')
    if len(checkAsinArr) > 0:
        checkfollowSeller(driver, checkAsinArr)
    else:
        driver.quit()
        print('=========请设置要检查的 asin ==============')


def checkfollowSeller(driver, checkAsinArr):
    global get_goods_num
    for index, goods in enumerate(checkAsinArr):
        orderID = '112-9865419-5024262'
        print('==%s========================%s===============' % (asin, get_goods_num))
        url = 'https://sellercentral.amazon.com/messaging/contact?orderID=%s&marketplaceID=ATVPDKIKX0DER' % orderID
        # url = 'https://sellercentral.amazon.com/messaging/contact?orderID=112-7937445-3778642&marketplaceID=ATVPDKIKX0DER'
        driver.get(url)
        driver.implicitly_wait(random.uniform(2.5, 3.3))
        try:
            driver.find_element_by_xpath('//*[@id="ayb-contact-buyer"]/div[2]/kat-box/div/kat-radiobutton[5]/div[2]/kat-label[1]/label/span[2]').click()
            print('other-----')
            sleep(random.uniform(1.5, 3.3))
        except IndexError as e:
            print('other')
            print(e)
        try:
            print('select++++++++++')
            driver.find_element_by_xpath('//*[@id="ayb-contact-buyer"]/div[3]/form/div[1]/div/kat-dropdown/div/div[1]/div/div[1]').click()
            sleep(random.uniform(1.5, 1.9))
            drop_down = driver.find_element_by_xpath('//*[@id="ayb-contact-buyer"]/div[3]/form/div[1]/div/kat-dropdown/div/div[2]/div')
            sleep(random.uniform(1.5, 1.9))
            drop_down.find_element_by_id('114').click()
            print('select---------')
            sleep(random.uniform(0.5, 2.3))
        except IndexError as e:
            print('select')
            print(e)
        try:
            sleep(random.uniform(2.5, 4.3))
            emailContent = '''
            Thank you for purchasing our magnetic eyelashes with eyeliner.
    
            We stand behind our products and would like to do everything to make you happy. 
            
            If you have any concerns or dissatisfaction about the item you received, please feel free to contact back any time. 
            
            We promise to figure out a solution and make you satisfied.
            
            Once again we appreciate to have a chance to serve you. 
            
            Sincerely,
            Johnny
            '''
            # driver.find_element_by_xpath('//*[@id="ayb-contact-buyer"]/div[3]/form/div[2]/div/div[1]/kat-textarea/textarea').sendKeys( "hi product orderID is error !!!");
            driver.find_element_by_xpath('//*[@id="ayb-contact-buyer"]/div[3]/form/div[2]/div/div[1]/kat-textarea/textarea').send_keys("hi product orderID is error !!!");
            print('texteara-------')
        except IndexError as e:
            print('texteara')
            print(e)
        try:
            sleep(random.uniform(1.5, 3.3))
            driver.find_element_by_xpath('//*[@id="ayb-contact-buyer"]/div[7]/kat-button/button').click()
            print('ok email')
        except IndexError as e:
            print('send email')
            print(e)
    time.sleep(random.uniform(33.5, 65.3))


    get_goods_num = get_goods_num + 1
    # checkfollowSeller(driver, checkAsinArr)
    # print(len(resList))





if __name__ == '__main__':
    getAsin()
